package com.yang.Offer;

import java.math.BigDecimal;

public class Offer15 {
}

class Offer15Solution {
    // you need to treat n as an unsigned value
    // 位运算，n&n-1 移除末位1一位
    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            n &= n - 1;
            count++;
        }
        return count;
    }

    // 涉及到大数运算，如果n太大会爆int
    public int hammingWeight2(int n) {
        int count = 0;
        while (n != 0) {
            if (n % 2 != 0) {
                count++;
            }
            n /= 2;
        }
        return count;
    }
}
